package com.sydx.service;

import com.github.pagehelper.PageHelper;
import com.sydx.dao.VipDao;
import com.sydx.entity.Vip;
import com.sydx.utils.BeanMapUtils;
import com.sydx.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class VipService {

    @Autowired
    private VipDao vipDao;

    public int create(Vip pi) {
        return vipDao.create(pi);
    }

    public int delete(Integer id) {
        // 原本这样写每次都要创建map,增加了耦合性，所以创建一个MapParameter工具类，直接调用
       /* HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        return clazzDao.delete(map);*/
        return vipDao.delete(MapParameter.getInstance().addId(id).getMap());
    }

    public int delete(String ids) {
        int flag = 0;
        for (String str : ids.split(",")) {
            flag = vipDao.delete(MapParameter.getInstance().addId(Integer.parseInt(str)).getMap());
        }
        return flag;
    }

    public int update(Vip vip) {
        Map<String,Object> map = MapParameter.getInstance()
                .add(BeanMapUtils.beanToMapForUpdate(vip))
                .addId(vip.getId()).getMap();
        return vipDao.update(map);
    }

    public List<Vip> query(Vip vip)  {
        /*分页查询*/
        if (vip != null && vip.getPage() != null) {
            PageHelper.startPage(vip.getPage(),vip.getLimit());
        }
        return vipDao.query(BeanMapUtils.beanToMap(vip));
    }

    public Vip detail(Integer id) {

        return vipDao.detail(MapParameter.getInstance().addId(id).getMap());
    }

    public int count(Vip vip) {

        return vipDao.count(BeanMapUtils.beanToMap(vip));
    }


}
